﻿using System.Diagnostics;
using Kaleida.ServiceMonitor.Model.Email;
using NUnit.Framework;

namespace Kaleida.UnitTests.Model.Email
{
    [TestFixture]
    public class HtmlMessageBuilderTests
    {
        [Test]
        public void TestAddHeading()
        {
            var builder = new HtmlMessageBuilder();
            builder.AddHeading("<b>Bold Heading</b>".AsRawHtml());
            builder.AddHeading("<Heading>".HtmlEncode());
            Trace.WriteLine(builder.ToString());
            Assert.IsTrue(builder.ToString().Contains("<h1><b>Bold Heading</b></h1>"));
            Assert.IsTrue(builder.ToString().Contains("<h1>&lt;Heading&gt;</h1>"));
        }

        [Test]
        public void TestAddLine()
        {
            var builder = new HtmlMessageBuilder();
            builder.AddLine("<b>Bold Content</b>".AsRawHtml());
            builder.AddLine("<Content>".HtmlEncode());
            Trace.WriteLine(builder.ToString());
            Assert.IsTrue(builder.ToString().Contains("<p><b>Bold Content</b></p>"));
            Assert.IsTrue(builder.ToString().Contains("<p>&lt;Content&gt;</p>"));
        }

        [Test]
        public void TestAddRaw()
        {
            var builder = new HtmlMessageBuilder();
            builder.AddRaw("<content>");
            Trace.WriteLine(builder.ToString());
            Assert.IsTrue(builder.ToString().Contains("<content>"));
        }

        [Test]
        public void TestAddRawFormat()
        {
            var builder = new HtmlMessageBuilder();
            builder.AddRawFormat("<content {0}>", 1);
            Trace.WriteLine(builder.ToString());
            Assert.IsTrue(builder.ToString().Contains("<content 1>"));
        }

        [Test]
        public void TestAddBullets()
        {
            var builder = new HtmlMessageBuilder();
            builder.AddBullets(new[] {"<content>".HtmlEncode(), "a&b".AsRawHtml()});
            Trace.WriteLine(builder.ToString());
            Assert.IsTrue(builder.ToString().Contains("<ul>\r\n  <li>&lt;content&gt;</li>\r\n  <li>a&b</li>\r\n</ul>"));
        }

        [Test]
        public void TestTableEncodesContentByDefault()
        {
            var builder = new HtmlMessageBuilder();

            var table = new BasicTable();
            table.AddHeaderRow(new[] {"<Column 1>", "<Column 2>"});
            table.AddRow(new[] { "A&B", "C&D" });
            
            builder.AddTable(table);

            Trace.WriteLine(builder.ToString());

            Assert.IsTrue(builder.ToString().Contains("<th>&lt;Column 1&gt;</th>"));
            Assert.IsTrue(builder.ToString().Contains("<th>&lt;Column 2&gt;</th>"));
            Assert.IsTrue(builder.ToString().Contains("<td>A&amp;B</td>"));
            Assert.IsTrue(builder.ToString().Contains("<td>C&amp;D</td>"));
        }

        [Test]
        public void TestRawHtmlCanBeAddedToTableUsingHtmlString()
        {
            var builder = new HtmlMessageBuilder();

            var table = new BasicTable();
            table.AddHeaderRow(new[] {"<b>Column 1</b>".AsRawHtml(), "<b>Column 2</b>".AsRawHtml()});
            table.AddRow(new[] {"<b>Bold</b>".AsRawHtml(), "<i>Italic</i>".AsRawHtml()});

            builder.AddTable(table);

            Assert.IsTrue(builder.ToString().Contains("<th><b>Column 1</b></th>"));
            Assert.IsTrue(builder.ToString().Contains("<th><b>Column 2</b></th>"));
            Assert.IsTrue(builder.ToString().Contains("<td><b>Bold</b></td>"));
            Assert.IsTrue(builder.ToString().Contains("<td><i>Italic</i></td>"));
        }

    }
}
